package JackSu.常用类.java中的比较器;

public class Goods implements  Comparable {


    private String name;
    private Double price;
    private String brand;

    public Goods() {
    }

    public Goods(String name, Double price, String brand) {
        this.name = name;
        this.price = price;
        this.brand = brand;
    }

    public Goods(String lenovoMouse, double i) {
        this.name=lenovoMouse;
        this.price=i;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
    //其实比较的时候这个tostring重写是非常有必要的，因为输入的时候使用了String.value（）
    // 实际上是调用了这个对象本身的tostring方法，如果不重写，返回的就是地址
    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", brand='" + brand + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        // 自己重写比较的规则  先判断类型
        if (o instanceof Goods) {
            Goods goods = (Goods) o;
            if (this.price > goods.price) {
                return 1;

            } else if (this.price < goods.price) {
                return -1;
            }else {
                //同价格按名称排序
                return this.name.compareTo(goods.name);
            }
        }
         throw  new RuntimeException("传入参数类型不一致");
    }
}
